﻿Imports XL.Common
Imports XL.Common.Utils
Imports XAuto.Facade
Imports System.Collections
Imports System.Data

Namespace Facade
    Public Class OpBasicMaster

        Public Shared Function GenerateDepartmentIDPath(ByVal departmentID As String, ByRef departmentDepth As Integer) As String

            Dim result As String = String.Empty
            Dim departmentRow As XAuto.FTs.FT_M_AV_DEPARTMENTRow
            Dim departmentCondition As New XAuto.Facade.AfBasicMaster.ConditionOfM_AV_DEPARTMENT(XL.DB.Utils.ConditionBuilder.LogicOperators.Logic_And)
            Dim departmentIDPath As New ArrayList

            departmentDepth = 0
            Do While departmentID.Length > 0

                departmentIDPath.Add(departmentID)
                departmentCondition.Clear()
                departmentCondition.Add(XAuto.Facade.AfBasicMaster.M_AV_DEPARTMENTColumns.DEPARTMENT_IDColumn, "=", departmentID)
                departmentRow = _
                    XAuto.Facade.AfBasicMaster.GetM_AV_DEPARTMENTRow(departmentCondition)

                If IsNothing(departmentRow) = True Then
                    Return String.Empty
                End If

                departmentDepth += 1
                departmentID = departmentRow.PARENT_DEPARTMENT

            Loop

            Dim counter As Integer

            For counter = departmentIDPath.Count - 1 To 0 Step -1
                result &= CommTK.FString(departmentIDPath(counter)) & "/"
            Next

            result = "/" & result

            Return result

        End Function

        Public Shared Sub SynchronizeDepartmentStaffWithSecureUser()

            Dim departmentList As New XAuto.FTs.FT_M_AV_DEPARTMENT
            Dim departmentRow As XAuto.FTs.FT_M_AV_DEPARTMENTRow
            Dim departmentCondition As New XAuto.Facade.AfBasicMaster.ConditionOfM_AV_DEPARTMENT(XL.DB.Utils.ConditionBuilder.LogicOperators.Logic_And)
            XAuto.Facade.AfBasicMaster.FillFT_M_AV_DEPARTMENT(Nothing, departmentList)

            Dim staffList As New XAuto.FTs.FT_M_AV_STAFF
            Dim staffRow As XAuto.FTs.FT_M_AV_STAFFRow
            Dim staffCondition As New XAuto.Facade.AfBasicMaster.ConditionOfM_AV_STAFF(XL.DB.Utils.ConditionBuilder.LogicOperators.Logic_And)
            XAuto.Facade.AfBasicMaster.FillFT_M_AV_STAFF(Nothing, staffList)

            Dim secureList As New XAuto.FTs.FT_S_SECURE
            Dim secureRow As XAuto.FTs.FT_S_SECURERow
            Dim secureCondition As New XAuto.Facade.AfSysConfig.ConditionOfS_SECURE(XL.DB.Utils.ConditionBuilder.LogicOperators.Logic_And)
            XAuto.Facade.AfSysConfig.FillFT_S_SECURE(Nothing, secureList)

            Dim userList As New XAuto.FTs.FT_S_USER
            Dim userRow As XAuto.FTs.FT_S_USERRow
            Dim userCondition As New XAuto.Facade.AfSysConfig.ConditionOfS_USER(XL.DB.Utils.ConditionBuilder.LogicOperators.Logic_And)
            XAuto.Facade.AfSysConfig.FillFT_S_USER(Nothing, userList)

            Dim secureID As String
            Dim userID As String
            Dim departmentCodes As New ArrayList
            Dim staffCodes As New ArrayList
            For Each departmentRow In departmentList

                departmentCodes.Add(departmentRow.DEPARTMENT_CODE)

                secureCondition.Clear()
                secureCondition.Add(AfSysConfig.S_SECUREColumns.SECURE_NAMEColumn, "=", departmentRow.DEPARTMENT_CODE)
                secureRow = XAuto.Facade.AfSysConfig.GetS_SECURERow(secureCondition)

                If IsNothing(secureRow) = True Then
                    secureID = System.Guid.NewGuid.ToString
                    Facade.OpSysConfig.CreateSecureInfo( _
                        secureID, _
                        departmentRow.DEPARTMENT_CODE, _
                        String.Empty)
                Else
                    secureID = secureRow.SECURE_ID
                End If

                XAuto.Facade.AfBasicMaster.ReviseM_AV_DEPARTMENTRow(departmentRow.DEPARTMENT_ID, AfBasicMaster.M_AV_DEPARTMENTColumns.SECURE_IDColumn, secureID)

                staffCondition.Clear()
                staffCondition.Add(AfBasicMaster.M_AV_STAFFColumns.DEPARTMENT_IDColumn, "=", departmentRow.DEPARTMENT_ID)

                For Each staffRow In staffList.FindRowsByCondition(staffCondition)

                    staffCodes.Add(staffRow.STAFF_CODE)
                    userCondition.Clear()
                    userCondition.Add(AfSysConfig.S_USERColumns.SECURE_IDColumn, "=", secureID)
                    userCondition.Add(AfSysConfig.S_USERColumns.USER_ACCOUNTColumn, "=", staffRow.STAFF_CODE)

                    userRow = XAuto.Facade.AfSysConfig.GetS_USERRow(userCondition)

                    If IsNothing(userRow) = True Then
                        userID = System.Guid.NewGuid.ToString
                        Facade.OpSysConfig.CreateUserInfo( _
                            userID, _
                            staffRow.STAFF_CODE, _
                            staffRow.STAFF_CODE, _
                            secureID, _
                            String.Empty)
                    Else
                        userID = userRow.USER_ID
                    End If

                    XAuto.Facade.AfBasicMaster.ReviseM_AV_STAFFRow(staffRow.STAFF_ID, AfBasicMaster.M_AV_STAFFColumns.USER_IDColumn, userID)
                Next

            Next


            secureCondition.Clear()
            secureCondition.Add(AfSysConfig.S_SECUREColumns.SECURE_NAMEColumn, False, departmentCodes)
            secureCondition.Add(AfSysConfig.S_SECUREColumns.SECURE_NAMEColumn, "<>", CommDecl.SYSTEM_DEFAULT_SECURE_NAME)
            XAuto.Facade.AfSysConfig.DeleteS_SECUREData(secureCondition)

            userCondition.Clear()
            userCondition.Add(AfSysConfig.S_USERColumns.USER_ACCOUNTColumn, False, staffCodes)
            userCondition.Add(AfSysConfig.S_USERColumns.USER_ACCOUNTColumn, "<>", CommDecl.SYSTEM_DEFAULT_ACCOUNT)

            XAuto.Facade.AfSysConfig.DeleteS_USERData(userCondition)

        End Sub

        Public Shared Function ValidateStaffPassword(ByVal staffCode As String, ByVal password As String) As Boolean


            Dim userCondition As New XAuto.Facade.AfSysConfig.ConditionOfS_USER(XL.DB.Utils.ConditionBuilder.LogicOperators.Logic_And)
            userCondition.Add(AfSysConfig.S_USERColumns.USER_ACCOUNTColumn, "=", staffCode)

            Dim userRow As XAuto.FTs.FT_S_USERRow = _
                XAuto.Facade.AfSysConfig.GetS_USERRow(userCondition)

            If IsNothing(userRow) = True Then
                Return False
            End If

            If Security.Encrypt(password) <> userRow.USER_PASSWORD Then
                Return False
            End If

            Return True
        End Function

        Public Shared Sub FillDepartmentListOfRunner( _
            ByVal staffID As String, _
            ByRef departmentList As XAuto.FTs.FT_M_AV_DEPARTMENT)

            Dim departmentCondition As New XAuto.Facade.AfBasicMaster.ConditionOfM_AV_DEPARTMENT(XL.DB.Utils.ConditionBuilder.LogicOperators.Logic_And)
            departmentCondition.Add(XAuto.Facade.AfBasicMaster.M_AV_DEPARTMENTColumns.RUNNERColumn, "=", staffID)

            XAuto.Facade.AfBasicMaster.FillFT_M_AV_DEPARTMENT(departmentCondition, departmentList)
        End Sub

        Public Shared Function GetAutoStaffCode(ByVal preview As Boolean, ByVal departmentID As String) As String

            Dim resultBuilder As New LineStrBuilder

            Dim departmentRow As XAuto.FTs.FT_M_AV_DEPARTMENTRow = _
                XAuto.Facade.AfBasicMaster.GetM_AV_DEPARTMENTRow(departmentID)

            If IsNothing(departmentRow) = True Then
                Return String.Empty
            End If

            Dim codePrefix As String = SysInfo.ReadShareSysInfo(Decls.SVN_STAFF_CODE_PREFIX)
            Dim codeDigitLength As Integer = CommTK.FInteger(SysInfo.ReadShareSysInfo(Decls.SVN_STAFF_CODE_LENGTH))
            'Dim hasClassifyCode As Boolean = CommTK.FBoolean(SysInfo.ReadShareSysInfo(Decls.SVN_ASSET_BARCODE_HAS_CLASSIFY_CODE))

            resultBuilder.AppendFormat(codePrefix)
            'If hasClassifyCode = True Then
            resultBuilder.AppendFormat(departmentRow.DEPARTMENT_CODE)
            'End If

            If preview = True Then
                resultBuilder.AppendFormat(StrDup(codeDigitLength, "X"))
                Return resultBuilder.ToString
            End If

            Dim seedName As String = String.Format("{0}_{1}", Decls.SPX_STAFF_CODE, resultBuilder.ToString)
            Dim seedValue As Integer = Facade.OpSysConfig.GetNewSeedID(seedName)

            resultBuilder.AppendFormat("{0:" & StrDup(codeDigitLength, "0") & "}", seedValue)

            Return resultBuilder.ToString

        End Function
    End Class
End Namespace

